﻿using Mono.Data.Sqlite;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Mono.Data.Sqlite;
using System.Data;

public class SetQuestionMgr : MonoBehaviour {
    public static SetQuestionMgr s_Instance;
    public List<SetQuestionQuestion> m_BankQuestionList = new List<SetQuestionQuestion>();
    public List<SetQuestionQuestion> m_QuestionList = new List<SetQuestionQuestion>();
    public SetQuestionQuestion m_SetQuestionPrefab;
    public SetQuestionOption m_SetQuestionOptionPrefab;
    public Transform m_BankContent, m_SetContent,m_OptionContent;
    public Image m_InputImg;

    public Text m_QuestionNumText, m_StemText,m_QuestionTypeText;

    private string m_editorName;
    private int m_examID;
    private SetQuestionQuestion m_setQuestionNow;
    // Use this for initialization
    void Awake () {
        s_Instance = this;

    }
    private void Start() {
        m_editorName = PlayerPrefs.GetString("EditorName", "555");
        m_examID = PlayerPrefs.GetInt("ExamID", 1);
        Init();
    }
    void Init() {
        string cmd = "select * from QUESTION_TABLE";
        SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);
        if (reader != null) {
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);
            if (dataTable.Rows.Count > 0) {
                for (int i = 0; i < dataTable.Rows.Count; i++) {
                    string num = dataTable.Rows[i]["ID"].ToString();
                    string stem = dataTable.Rows[i]["Stem"].ToString();
                    int type = (int)dataTable.Rows[i]["Type"];
                    string picPath = dataTable.Rows[i]["Pic_Path"].ToString();
                    SetQuestionQuestion option = Instantiate(m_SetQuestionPrefab) as SetQuestionQuestion;
                    option.m_NumberText.text = num;
                    option.m_StemText.text = stem;
                    option.m_Type = type;
                    option.m_PicPath = picPath;
                    option.transform.SetParent(m_BankContent);
                    option.transform.localScale = new Vector3(1f, 1f, 1f);
                    m_BankQuestionList.Add(option);
                }
            }
        }
        FreshSetQuestion();
    }
    void FreshSetQuestion() {
        m_QuestionList.Clear();
        for (int i = 0; i < m_SetContent.childCount; i++) {
            Destroy(m_SetContent.GetChild(i).gameObject);
        }
        string cmd1 = "select * from QUESTION_TABLE where ID in (select Question_ID from PAPER_QUESTION_TABLE where Paper_ID = " + m_examID.ToString() + " )";
        SqliteDataReader reader1 = SQLManager.s_Instance.ExecuteSQLCommand(cmd1);
        DataTable dataTable1 = new DataTable();
        dataTable1.Load(reader1);
        if (dataTable1.Rows.Count > 0) {
            for (int i = 0; i < dataTable1.Rows.Count; i++) {
                string num = dataTable1.Rows[i]["ID"].ToString();
                string stem = dataTable1.Rows[i]["Stem"].ToString();
                int type = (int)dataTable1.Rows[i]["Type"];
                string picPath = dataTable1.Rows[i]["Pic_Path"].ToString();
                SetQuestionQuestion option = Instantiate(m_SetQuestionPrefab) as SetQuestionQuestion;
                option.m_NumberText.text = num;
                option.m_StemText.text = stem;
                option.m_Type = type;
                option.m_PicPath = picPath;
                option.transform.SetParent(m_SetContent);
                option.transform.localScale = new Vector3(1f, 1f, 1f);
                option.m_IsSetedQuestion = true;
                m_QuestionList.Add(option);
            }
        }
    }
    public void OnSetQuestionClick(SetQuestionQuestion question) {
        foreach(var item in m_BankQuestionList) {
            item.SetState(false);
        }
        foreach (var item in m_QuestionList)
        {
            item.SetState(false);
        }
        m_setQuestionNow = question;
        question.SetState(true);
        m_QuestionNumText.text = question.m_NumberText.text;
        m_StemText.text = question.m_StemText.text;
        int type = question.m_Type;
        switch (type) {
            case 0:
                m_QuestionTypeText.text = "未知类型";
                break;
            case 1:
                m_QuestionTypeText.text = "单选题";
                break;
            case 2:
                m_QuestionTypeText.text = "多选题";
                break;
            case 3:
                m_QuestionTypeText.text = "填空题";
                break;
        }
        LoadPicture(question.m_PicPath);

        for(int i = 0; i < m_OptionContent.childCount; i++) {
            Destroy(m_OptionContent.GetChild(i).gameObject);
        }
        string cmd = "select * from OPTION_TABLE where QID = '"+ question.m_NumberText.text+"'";
        SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);
        if (reader != null) {
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);
            if (dataTable.Rows.Count > 0) {
                for (int i = 0; i < dataTable.Rows.Count; i++) {
                    string stem = dataTable.Rows[i]["Stem"].ToString();
                    int score = (int)dataTable.Rows[i]["Score"];
                    int jumpID = (int)dataTable.Rows[i]["Jump_ID"];
                    SetQuestionOption option1 = Instantiate(m_SetQuestionOptionPrefab) as SetQuestionOption;
                    option1.Init(score,stem,jumpID);
                    option1.transform.SetParent(m_OptionContent);
                    option1.transform.localScale = new Vector3(1f,1f,1f);
                }
            }
        }

    }
    public void OnClickInsertQuestion() {
        if (m_setQuestionNow == null) {
            return;
        }

        string cmd = "INSERT INTO PAPER_QUESTION_TABLE VALUES ('" +
           m_examID.ToString() + "','" +
           m_setQuestionNow.m_NumberText.text + "')";
        SQLManager.s_Instance.ExecuteSQLCommand(cmd);
        FreshSetQuestion();
    }
    public void OnClickDeleteQuestion() {
        if (m_setQuestionNow == null || !m_setQuestionNow.m_IsSetedQuestion) {
            return;
        }
        string cmd = "DELETE FROM PAPER_QUESTION_TABLE WHERE Question_ID = '" + m_setQuestionNow.m_NumberText.text + "'";
        SQLManager.s_Instance.ExecuteSQLCommand(cmd);
        FreshSetQuestion();
    }
    public void OnClickHomeButton() {
        SceneManager.LoadScene("Scene");
    }
    void LoadPicture(string path) {
        if (path.Length < 1) {
            return;
        }
        StartCoroutine(Load(path));
    }
    IEnumerator Load(string path) {
        WWW www = new WWW("file:///" + path);
        yield return www;

        while (!www.isDone) {

        }
        if (www.error != null) {
            print(www.error);
            m_InputImg.sprite = null;
        } else {
            Texture2D texture = www.texture;
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            m_InputImg.sprite = sprite;
        }

    }
}
